package com.saturday.uid;

import com.saturday.uid.config.BitsAllocatorProperties;
import com.saturday.uid.config.UidEpochProperties;
import com.saturday.uid.config.UidRingBufferProperties;
import com.saturday.uid.config.ZookeeperProperties;
import com.saturday.uid.impl.CachedUidGenerator;
import com.saturday.uid.worker.dao.WorkerNodeDao;
import com.saturday.uid.worker.dao.impl.ZkNodeWorkerNodeDaoImpl;
import com.saturday.uid.worker.service.WorkerIdAssigner;
import com.saturday.uid.worker.service.impl.RepeatableWorkerIdAssigner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(value = {BitsAllocatorProperties.class, UidEpochProperties.class,
        UidRingBufferProperties.class, ZookeeperProperties.class})
@ConditionalOnWebApplication
public class UidAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(name = "uidGenerator")
    UidGenerator uidGenerator() {
        return new CachedUidGenerator();
    }

    @Bean
    @ConditionalOnMissingBean(name = "workerIdAssigner")
    WorkerIdAssigner workerIdAssigner() {
        return new RepeatableWorkerIdAssigner();
    }

    @Bean
    @ConditionalOnMissingBean(name = "workerNodeProxy")
    WorkerNodeDao workerNodeProxy() {
        return new ZkNodeWorkerNodeDaoImpl();
    }
}
